package com.elong.businesstravel.base.uploadpic;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.utils.ImageUtil;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.ContactsContract.Contacts.Data;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

/**
 * 用于上传照片，通过照相机，或者通过相册
 * 
 * @author hongfeijia
 * 
 */
public class UploadPic {

	/**
	 * 照相机或者gallery获取图片成功
	 * 
	 * @author lenovo
	 * 
	 */
	public interface OnGetPicSucceed {
		public void onGetPicSucceed(String picPathString);
	}

	public static final String IMAGE_CAPTURE_NAME = "cameraTmp.png"; // 照片名称
	public static final String SDCARD_ROOT_PATH = Environment
			.getExternalStorageDirectory().getAbsolutePath();
	public static final String CLT = "clt";
	public static final String FILE_NAME = "image";

	/** 用来标识请求照相功能的activity */
	public static final int CAMERA_WITH_DATA = 3023;
	/** 用来标识请求gallery的activity */
	public static final int PHOTO_PICKED_WITH_DATA = 3021;

	private Activity mActivity;
	/** 照片的名字以时间命名 */
	public String mCameraPicNameString;

	private OnGetPicSucceed mOnGetPicSucceed;

	public UploadPic(Activity activity) {
		mActivity = activity;
	}

	public UploadPic() {
	}

	public void setOnGetPicSucceed(OnGetPicSucceed onGetPicSucceed) {
		mOnGetPicSucceed = onGetPicSucceed;
	}

	/**
	 * 照相或者在Gallery中选择
	 */
	public void doPickPhotoAction() {

		// SelectPicDialog.getUpSelectPicDialog(mActivity)
		// .setCameraOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View view) {
		// String status = Environment.getExternalStorageState();
		// if (status.equals(Environment.MEDIA_MOUNTED)) {// 判断是否有SD卡
		// doTakePhoto();// 用户点击了从照相机获取
		// } else {
		// HandlerToastUI
		// .getHandlerToastUI(mActivity, "没有SD卡");
		// }
		// }
		// }).setGalleryOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View view) {
		// doPickPhotoFromGallery();// 从相册中去获取
		// }
		// }).show();
	}

	/**
	 * 因为调用了Camera和Gally所以要判断他们各自的返回情况,他们启动时是这样的startActivityForResult
	 * 
	 * @param uri
	 * @return
	 */
	private String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = mActivity.managedQuery(uri, projection, null, null,
				null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

	/**
	 * 返回照相机存储图片的路径
	 * 
	 * @return
	 */
	public String getCameraPath() {

		String imagePathString = SDCARD_ROOT_PATH + "/" + CLT + "/" + FILE_NAME
				+ "/" + mCameraPicNameString + ".png";

		return imagePathString;
	}

	public String getPhonePath(Intent data) {
		Uri uri = data.getData();
		String selectedImagePath = getPath(uri);
		return selectedImagePath;
	}

	/**
	 * 拍照获取图片,进行裁剪
	 * 
	 */
	public void doTakePhotoCrop() {

		try {
			SimpleDateFormat format = new SimpleDateFormat(
					"yyyy-MM-dd-HH-mm-ss");
			mCameraPicNameString = format.format((new Date()));

			Log.e("doTakePhoto mCameraPicNameString", mCameraPicNameString);

			File sohuAutoFile = new File(SDCARD_ROOT_PATH + "/" + CLT);
			if (!sohuAutoFile.exists()) {
				sohuAutoFile.mkdirs();
			}

			File picDirFile = new File(sohuAutoFile.getAbsolutePath() + "/"
					+ FILE_NAME);
			if (!picDirFile.exists()) {
				picDirFile.mkdirs();
			}

			// 创建文件
			File picFile = new File(picDirFile.getAbsolutePath() + "/",
					mCameraPicNameString + ".png");
			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			// 存储卡可用 将照片存储在 sdcard
			intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(picFile));
			intent.putExtra("crop", "true");
			intent.putExtra("aspectX", 1);
			intent.putExtra("aspectY", 1);
			intent.putExtra("outputX", 512);
			intent.putExtra("outputY", 512);
			mActivity.startActivityForResult(intent, CAMERA_WITH_DATA);
		} catch (ActivityNotFoundException e) {
			HandlerToastUI.getHandlerToastUI(mActivity,
					"R.string.photoPickerNotFoundText");
		}
	}

	/**
	 * 拍照获取图片
	 * 
	 */
	public void doTakePhoto() {

		try {
			SimpleDateFormat format = new SimpleDateFormat(
					"yyyy-MM-dd-HH-mm-ss");
			mCameraPicNameString = format.format((new Date()));

			Log.e("doTakePhoto mCameraPicNameString", mCameraPicNameString);

			File sohuAutoFile = new File(SDCARD_ROOT_PATH + "/" + CLT);
			if (!sohuAutoFile.exists()) {
				sohuAutoFile.mkdirs();
			}

			File picDirFile = new File(sohuAutoFile.getAbsolutePath() + "/"
					+ FILE_NAME);
			if (!picDirFile.exists()) {
				picDirFile.mkdirs();
			}

			// 创建文件
			File picFile = new File(picDirFile.getAbsolutePath() + "/",
					mCameraPicNameString + ".png");


			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			// 存储卡可用 将照片存储在 sdcard
			intent.putExtra(MediaStore.EXTRA_OUTPUT, /* photoUri */
					Uri.fromFile(picFile));
			mActivity.startActivityForResult(intent, CAMERA_WITH_DATA);
		} catch (ActivityNotFoundException e) {
			HandlerToastUI.getHandlerToastUI(mActivity,
					"R.string.photoPickerNotFoundText");
		}
	}

	/**
	 * 请求Gallery程序
	 */
	private void doPickPhotoFromGallery() {
		try {

			Intent picture = new Intent(
					Intent.ACTION_PICK,
					android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			mActivity.startActivityForResult(picture, PHOTO_PICKED_WITH_DATA);

		} catch (ActivityNotFoundException e) {
			HandlerToastUI.getHandlerToastUI(mActivity,
					"R.string.photoPickerNotFoundText");
		}
	}

	public static final String[] STORE_IMAGES = {
			MediaStore.Images.Media.DISPLAY_NAME,// 显示的名字
			MediaStore.Images.Media.DATA,// 显示路径
			MediaStore.Images.Media.LONGITUDE,// 经度
			MediaStore.Images.Media._ID,// id
			MediaStore.Images.Media.BUCKET_ID,// dir id 目录
			MediaStore.Images.Media.BUCKET_DISPLAY_NAME,// dir name 目录名字
			MediaStore.Images.Media.ORIENTATION };

	public static final String[] THUMBNAIL_PROJECTION = new String[] {
			Images.ImageColumns._ID, Images.ImageColumns.DATE_MODIFIED,
			Images.ImageColumns.DATA, Images.ImageColumns.ORIENTATION,
			MediaStore.Images.Media.DATA };

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {

		case CAMERA_WITH_DATA: {
			switch (resultCode) {
			case Activity.RESULT_OK: {

				String cameraPath = getCameraPath();
				
				DebugLog.logI("CAMERA_WITH_DATA : " + cameraPath);
				if (null != mOnGetPicSucceed) {
					mOnGetPicSucceed.onGetPicSucceed(cameraPath);
				}
				break;
			}
			case Activity.RESULT_CANCELED: {
				break;
			}
			default:
				break;
			}
			break;
		}
		case PHOTO_PICKED_WITH_DATA: {
			switch (resultCode) {
			case Activity.RESULT_OK: {
				String selectedImagePath = getPhonePath(data);
				DebugLog.logI("PHOTO_PICKED_WITH_DATA cameraPath : "
						+ selectedImagePath);
				if (null != mOnGetPicSucceed) {
					mOnGetPicSucceed.onGetPicSucceed(selectedImagePath);
				}
				break;
			}
			case Activity.RESULT_CANCELED: {

				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
	}

}
